
package com.kzd.sdk.ui.fragment;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kzd.sdk.base.OwnerFragment;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.callback.KZD_OnAccessListener;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.utils.AppUtils;

public class KZD_AccessPhoneFragment extends OwnerFragment {


    private EditText etAccessPhoneMobile, etAccessPhoneCode;
    private TextView tvAccessPhoneCodeGet;

    private String mSessionId = "";

    private final CountDownTimer mCountDownTimer = new CountDownTimer(1000 * 60, 1000) {
        public void onTick(long millisUntilFinished) {
            String text = "剩余" + millisUntilFinished / 1000 + "秒";
            tvAccessPhoneCodeGet.setText(text);
        }

        public void onFinish() {
            tvAccessPhoneCodeGet.setTextColor(Color.parseColor("#2983FF"));
            tvAccessPhoneCodeGet.setEnabled(true);
            tvAccessPhoneCodeGet.setText("获取验证码");
        }
    };

    private final KZD_HttpCallback<String> callbackPhoneCode = new KZD_HttpCallback<String>() {
        @Override
        public void onSuccess(String data) {
            mSessionId = data;
            mCountDownTimer.start();
            tvAccessPhoneCodeGet.setTextColor(Color.parseColor("#999999"));
            tvAccessPhoneCodeGet.setEnabled(false);
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    public KZD_OnAccessListener mOnAccessListener;

    public void setOnAccessListener(KZD_OnAccessListener onAccessListener) {
        this.mOnAccessListener = onAccessListener;
    }

    @Override
    public String getLayoutIdByName() {
        return "kzd_fragment_access_phone";
    }

    @Override
    public void initView(View view) {
        etAccessPhoneMobile = view.findViewById(AppUtils.getId(mContext, "etAccessPhoneMobile"));
        etAccessPhoneCode = view.findViewById(AppUtils.getId(mContext, "etAccessPhoneCode"));
        tvAccessPhoneCodeGet = view.findViewById(AppUtils.getId(mContext, "tvAccessPhoneCodeGet"));

        view.findViewById(AppUtils.getId(mContext, "ivAccessPhoneBack"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnClickListener != null) {
                            mOnClickListener.onClick(v);
                        }
                    }
                });

        tvAccessPhoneCodeGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etAccessPhoneMobile.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    toast("请输入手机号");
                } else {
                    KZD_ApiManager.httpPhoneCode(phone, 0, callbackPhoneCode);
                }
            }
        });

        view.findViewById(AppUtils.getId(mContext, "tvAccessPhoneSubmit"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String phone = etAccessPhoneMobile.getText().toString();
                        String code = etAccessPhoneCode.getText().toString();
                        if (TextUtils.isEmpty(phone)) {
                            toast("请输入手机号");
                            return;
                        }
                        if (TextUtils.isEmpty(code)) {
                            toast("请输入验证码");
                            return;
                        }
                        if (mOnAccessListener != null) {
                            mOnAccessListener.onPhone(phone, code,mSessionId);
                        }
                    }
                });
    }

    @Override
    public void onStop() {
        super.onStop();
        mCountDownTimer.cancel();
    }
}
